//
//  CookingViewController.m
//  Menu
//
//  Created by YouLu on 15/7/3.
//  Copyright (c) 2015年 lijiangang. All rights reserved.
//

#import "CookingViewController.h"

@interface CookingViewController ()

@end

@implementation CookingViewController
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    if (self.isCollect == NO) {
        self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"收藏" style:UIBarButtonItemStylePlain target:self action:@selector(rightButtonAction)] autorelease];
        self.navigationItem.rightBarButtonItem.tintColor = [UIColor blackColor];
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = self.foodName;
    self.topImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 64, MyWidth, 260*HEIGHT)];
    self.topImageView.backgroundColor = [UIColor whiteColor];
    self.topImageView.userInteractionEnabled =YES;
    [self.view addSubview:self.topImageView];
    [_topImageView release];
    [self.topImageView sd_setImageWithURL:[NSURL URLWithString:self.imagePathThumbnails]];
    NSMutableArray *array = [NSMutableArray arrayWithObjects:@"所需材料",@"制作步骤",@"相关知识",@"相宜相克", nil];
    UISegmentedControl *segmentC = [[UISegmentedControl alloc] initWithItems:array];
    segmentC.frame = CGRectMake(0, 260*HEIGHT+64, MyWidth, 40*HEIGHT);
    segmentC.tintColor = [UIColor lightGrayColor];
    segmentC.userInteractionEnabled = YES;
    segmentC.selectedSegmentIndex = 0;
    [segmentC addTarget:self action:@selector(cookingSegmentAction:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:segmentC];
    [segmentC release];

    self.stuffVC = [[StuffViewController alloc] init];
    self.stuffVC.view.frame = CGRectMake(0, 64+260*HEIGHT+40*HEIGHT, MyWidth, MyHeight-64-49-260*HEIGHT-40*HEIGHT);
    [self.stuffVC loadData:self.vegetable_id];
    
    self.stepVC = [[StepViewController alloc] init];
    self.stepVC.view.frame = CGRectMake(0, 64+260*HEIGHT+40*HEIGHT, MyWidth, MyHeight-64-49-260*HEIGHT-40*HEIGHT);
    [self.stepVC loadData:self.vegetable_id];
    
    self.knowledgeVC = [[KnowledgeViewController alloc] init];
    self.knowledgeVC.view.frame =CGRectMake(0, 64+260*HEIGHT+40*HEIGHT, MyWidth, MyHeight-64-49-260*HEIGHT-40*HEIGHT);
    [self.knowledgeVC loadData:self.vegetable_id];
    
    self.fittingVC = [[FittingViewController alloc] init];
    self.fittingVC.view.frame = CGRectMake(0, 64+260*HEIGHT+40*HEIGHT, MyWidth, MyHeight-64-49-260*HEIGHT-40*HEIGHT);
    [self.fittingVC loadData:self.vegetable_id];
    
    [self addChildViewController:self.stuffVC];
    [self addChildViewController:self.stepVC];
    [self addChildViewController:self.knowledgeVC];
    [self addChildViewController:self.fittingVC];
    [self.view addSubview:self.stuffVC.view];
    [self.view addSubview:self.stepVC.view];
    [self.view addSubview:self.knowledgeVC.view];
    [self.view addSubview:self.fittingVC.view];
    [self.view bringSubviewToFront:self.stuffVC.view];
    [_stuffVC release];
    [_stepVC release];
    [_knowledgeVC release];
    [_fittingVC release];



}
-(void)rightButtonAction
{
    CollectModel *model = [[CollectModel alloc] init];
    model.foodName = self.foodName;
    model.typeID = self.vegetable_id;
    model.picUrl = self.imagePathThumbnails;
    [[DataHandle shareData] openDataBase];
    [[DataHandle  shareData] createCollectInfoTable];
    NSMutableArray *collectArray = [[DataHandle shareData] selectCollectInfoTabel];
    NSMutableArray *nameArray = [NSMutableArray array];
    for (CollectModel *model in collectArray) {
        [nameArray addObject:model.foodName];
    }
    if ([nameArray containsObject:model.foodName]) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您已收藏" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确认", nil];
        [alertView show];
    }else{
        [[DataHandle shareData] insertCollectInfo:model];
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"收藏成功" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确认", nil];
        [alertView show];
    }
    
    
}
-(void)cookingSegmentAction:(UISegmentedControl *)segment
{
    if (segment.selectedSegmentIndex == 0) {
        [self.view bringSubviewToFront:self.stuffVC.view];
        
    }
    if (segment.selectedSegmentIndex == 1) {
        [self.view bringSubviewToFront:self.stepVC.view];


    }
    if (segment.selectedSegmentIndex == 2) {
        [self.view bringSubviewToFront:self.knowledgeVC.view];


    }
    if (segment.selectedSegmentIndex == 3) {
        [self.view bringSubviewToFront:self.fittingVC.view];


    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
